﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Data.OleDb;
using System.Data;
using System.Configuration;
using System.Text.RegularExpressions;

namespace qdata
{
   public  class Common
    {
       private static volatile Common m_instance = null;

       public static Common GetInstance()
        {
            if (m_instance == null)
            {
                lock (typeof(Common))
                {
                    if (m_instance == null)
                    {

                        m_instance = new Common();
                    }
                }
            }
            return m_instance;
        }

        public string GetHtmlCode(string url)
        {
            try
            {
                HttpWebRequest httpReq;
                HttpWebResponse httpResp;
                Uri httpurl = new System.Uri(url);

                httpReq = (HttpWebRequest)WebRequest.Create(httpurl);
                httpReq.Method = "GET";
                httpReq.KeepAlive = false; // 获取或设置一个值，该值指示是否与Internet资源建立持久连接。
                httpResp = (HttpWebResponse)httpReq.GetResponse();
                StreamReader reader = new StreamReader(httpResp.GetResponseStream(), GetLgFormat());//System.Text.Encoding.GetEncoding("GB2312"));   //用GB2312字符
                string respHTML = reader.ReadToEnd();
                return respHTML;
            }
            catch
            {
                return string.Empty;
            }
        }
        private static string _GetLgFormat = string.Empty;
        public static string encoding
        {
            get
            {
                return _GetLgFormat;
            }
            set
            {
                _GetLgFormat = value;
            }
        }
        public Encoding GetLgFormat()
        {
            if (ConfigurationSettings.AppSettings["encoding"].ToString() == "UTF-8")
            {
                return Encoding.UTF8;
            }
            return Encoding.GetEncoding("GB2312");

        }

        string connectionString = ConfigurationSettings.AppSettings["connectionstring"].ToString();
        /// <summary>
        /// 执行SQL,返回DataTable
        /// </summary>
        /// <param name="Sql">SQL</param>
        /// <returns></returns>
        public DataTable ExecuteDataTable(string Sql)
        {

            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                try
                {
                    string sqlCommand = Sql;
                    using (OleDbCommand cmd = new OleDbCommand(sqlCommand, connection))
                    {
                        connection.Open();
                        OleDbDataAdapter adp = new OleDbDataAdapter(sqlCommand, connection);
                        adp.SelectCommand = cmd;
                        DataSet ds = new DataSet();
                        adp.Fill(ds);
                        return ds.Tables[0];
                    }
                }
                catch (Exception ex)
                {
                    if (connection.State == ConnectionState.Open)
                    {
                        connection.Close();
                    }
                    return null;
                }
            }
        }


        public string GetUrl(string v)
        {
            string pattern = @"(?is)(?<=href\s*=\s*(['""]?)\s*)[^'""\s>]+(?=\s*\1)";
            var s2 = Regex.Matches(v, pattern);
            if (s2.Count > 0)
            {
                string c = s2[0].Value;
                return c;
            }
            return string.Empty;
        }


    }
}
